<template>
	<view>
		<view class="top">
			<view v-if="loginShow">
				<view class="flex-center">
					<u--image  src="/static/pic/头像.png" shape="circle" width="100rpx" height="100rpx"></u--image>
				</view>
				<view class="flex-center">
					<u-button type="primary" text="用户登录" @click="login"></u-button>
				</view>
				<!-- <view class="flex-center">
					<button open-type="getPhoneNumber" @getphonenumber="getPhoneNumber">获取手机号</button>
				</view> -->
				<u-popup :show="popShow">
					<view>
						<u-button type="primary" open-type="getPhoneNumber" @getphonenumber="getPhoneNumber">手机号一键登录</u-button>
					</view>
					<view>
						<u-button type="success" text="账号密码登录" @click="gotoLogin"></u-button>
					</view>
				</u-popup>
			</view>
			<view v-if="userinfoShow">
				<u-row gutter="10">
					<u-col span="3">
						<view >用户名</view>
					</u-col>
					<u-col span="7">
						<view >{{loginUser.name}}</view>
					</u-col>
				</u-row>
				
				<u-row gutter="10">
					<u-col span="3">
						<view >手机号</view>
					</u-col>
					<u-col span="7">
						<view >{{loginUser.phoneNumber}}</view>
					</u-col>
				</u-row>
				<view>
					<u-button type="primary" text="退出登录"  @click="doexit" ></u-button>
				</view>
			</view>
			
			<!-- <uni-popup ref="popup" type="dialog" :show="dialogShow"> -->
			<!-- <uni-popup  :show="popShow">
				<view>123456</view>
			</uni-popup> -->
			<!-- <u-popup :show="popShow" :round="10" mode="bottom" @close="popClose" @open="popOpen">
				<view>
					<button open-type="getPhoneNumber" @getphonenumber="getPhoneNumber">手机号一键登录</button>
				</view>
			</u-popup> -->
		</view>
		<!-- <view>
			{{nick}}
		</view> -->
	</view>
</template>

<script>
	export default {
		data() {
			return {
				loginShow: false,
				userinfoShow: false,
				nick: 'xsb',
				popShow: false,
				dialogShow: false,
				loginUser:{}
				
			}
		},
		methods: {
			doexit(){
				this.loginShow = true;
				this.userinfoShow = false;
				uni.setStorageSync('token',null);
				uni.setStorageSync('userinfo',null);
			},
			gotoLogin(){
				this.dialogShow = false;
				uni.navigateTo({
					url:"/pages/userlogin/userlogin"
				})
			},
			popClose(){
				this.popShow = false;
			},
			popOpen(){
				this.popShow = true;
			},
			getPhoneNumber(e) {
				this.dialogShow = false;
				//调取本机手机号
				console.log('##',e);

				if (e.detail.errMsg === 'getPhoneNumber:ok') {
				  // 发起网络请求
				  wx.login({
					success: function(res) {
						console.log('##',res)
					 //  if (res.code) {
						// // 发起网络请求
						// wx.request({
						//   url: 'YOUR_BACKEND_URL', // 你的后端接口
						//   data: {
						// 	code: res.code,
						// 	...e.detail.iv, // 解密需要的参数
						// 	...e.detail.encryptedData // 解密需要的参数
						//   },
						//   success: function(res) {
						// 	console.log('服务器返回:', res.data);
						//   }
						// });
					 //  } else {
						// console.log('登录失败！' + res.errMsg);
					 //  }
					}
				  });
				} else {
				  console.log('用户未授权');
				}
			},
			userinfo(){
				uni.getUserProfile({
				  desc: '用于完善用户资料', // 必填，用户授权原因
				  success: (res) => {
				    const userInfo = res.userInfo; // 用户信息
				    console.log('用户信息:', userInfo);
				  },
				  fail: (err) => {
				    console.error('授权失败:', err);
				    uni.showToast({ title: '授权失败', icon: 'none' });
				  }
				});
			},
			login(){
				this.nick = 'xsb123';
				this.popShow = true;
				
				uni.login({
				  provider: 'weixin', //使用微信登录
				  onlyAuthorize:true,
				  success: loginRes=> {
				    console.log(loginRes);
					console.log(loginRes.authResult);
					
					// uni.getUserInfo({
					//       provider: 'weixin',
					//       success: infoRes =>{
					//         //console.log('用户昵称为：' + infoRes.userInfo.nickName);
					// 		console.log(infoRes);
					// 		this.nick = infoRes.userInfo.nickName;
					// 		//console.log('用户昵称为：' + infoRes.userInfo.nickName);
					//       }
					//     });

				  }
				});
				// uni.login({
				//   provider: 'weixin', // 或者 'snsapi_login_with_cellphone'
				//   success(res) {
				//     if (res.code) { // 获取临时code
				//       let phoneRes = uni.getUserInfo({ // 获取用户信息
				//         fields: ['nickname', 'avatarUrl', 'phoneNumber']
				//       })
				//       console.log(phoneRes.phoneNumber) // 手机号码
				//     }
				//   },
				//   fail(err) {
				//     console.error(err)
				//   }
				// });
			}
		},
		//
		onShow() {
			if(uni.getStorageSync('token')){
				this.loginShow = false;
				this.userinfoShow = true;
				this.loginUser = uni.getStorageSync('userinfo');
				console.log('###',this.loginUser)
			}else{
				this.loginShow = true;
				this.userinfoShow = false;
			}
			// console.log('token',uni.getStorageSync('token'));
			// console.log('userinfo',uni.getStorageSync('userinfo'));
			
		}
	}
</script>

<style lang="scss">
.top{
	
	background-color: 7f7f1;
	height: 200rpx;
	margin: 20rpx;
	padding: 20rpx;
	box-shadow: 5px 5px 10px rgba(0, 0, 0, 0.5);
}
.flex-center {
	display: flex;
	justify-content: center; /* 水平居中 */
}
</style>
